package com.javandad.app.client.project;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.javandad.app.client.CodesvrParam;
import com.javandad.app.client.cells.ProjectCell;
import com.javandad.app.shared.entity.User;
import com.javandad.app.shared.entity.project.ProjectAssoc;

public class Projects implements EntryPoint {

	RootPanel container;
	VerticalPanel projects;
	CellList<ProjectAssoc> list;
	Button createANewProject;
	

	ProjectServiceAsync pjs;
	String view = null;
	User user = null;
	
	private CreateNewProjectDialogBox dlg = new CreateNewProjectDialogBox(false, true, this);
	
	

	@Override
	public void onModuleLoad() {
	
		container = RootPanel.get("ProjectsContainer");
		if (container == null)
			return;
		pjs = GWT.create(ProjectService.class);
		view = Window.Location.getParameter("view");
		initGUI();
	}

	private void initGUI() {
		
		projects = new VerticalPanel();
	
		
		createANewProject = new Button("Add");
		createANewProject.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
			    dlg.show();	
			}
		});
		
		createANewProject.setSize("80px", "30px");
		createANewProject.setVisible(false);
		list = new CellList<ProjectAssoc>(new ProjectCell());
		final SingleSelectionModel<ProjectAssoc> selectionModel = new SingleSelectionModel<ProjectAssoc>();
		list.setSelectionModel(selectionModel);

		selectionModel
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
					public void onSelectionChange(SelectionChangeEvent event) {
						ProjectAssoc projectAssoc = selectionModel
								.getSelectedObject();
						String href = "project.jsp?"
								+ CodesvrParam.getCodesvrParam() + "&name="
								+ projectAssoc.getProjectname()
								+ (view != null ? "&view=" + view : "");
						Window.open(href, "_self", "");
					}
				});
		projects.add(createANewProject);
       
		projects.add(list);
		container.add(projects);
		loadUser();
	}

	protected void createANewProject(int level, final String projectName,
			int type, String description, String plink, String rlink) {

		pjs.createProject(user.getUsername(), level, projectName, type, description,
				plink, rlink, new AsyncCallback<Boolean>() {
					public void onFailure(Throwable caught) {
						Window.alert("createProject failed:\n"
								+ caught.getMessage());
					}

					public void onSuccess(Boolean result) {
						if (result) {
							Window
									.alert("Project " + projectName
											+ " created.");
							updateProjects(0);
						} else
							Window.alert("Project " + projectName
									+ " already exists!");
					}
				});

	}

	private void loadUser() {
		pjs.getUser(new AsyncCallback<User>() {
			public void onFailure(Throwable caught) {
				Window.alert("loadUser failed:\n" + caught.getMessage());
			}

			public void onSuccess(User result) {
				user = result;
				updateProjects(0);
				if (user.getUsername().equals(view))
					createANewProject.setVisible(true);
			}

		});
	}

	private void updateProjects(int level) {
		pjs.getProjectAssociations(view, level,
				new AsyncCallback<List<ProjectAssoc>>() {

					@Override
					public void onFailure(Throwable caught) {
					}

					@Override
					public void onSuccess(List<ProjectAssoc> result) {
						list.setRowData(result);
					}
				});
	}
}
